<?php

class HTTP
{
    const JSON = 'json';
    const RAW = 'raw';

    public function __construct()
    {

    }

    public function get($url, $params = array(), $type = 'raw', $loop = true)
    {
        $response = null;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url . '?' . http_build_query($params));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($curl);
        if ($loop) {
            while ($response == false) {
                echo curl_error($curl);
                sleep(10);
                $response = curl_exec($curl);
            }
        }
        curl_close($curl);
        return $this->parse($response, $type);
    }

    public function post($url, $params = array(), $post = array(), $type = 'raw')
    {
        $response = null;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url . '?' . http_build_query($params));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
        $response = curl_exec($curl);
        while ($response == false) {
            echo curl_error($curl);
            sleep(10);
            $response = curl_exec($curl);
        }
        curl_close($curl);
        return $this->parse($response, $type);
    }

    protected function parse($response, $type = 'raw')
    {
        switch($type) {
            case self::JSON: return json_decode($response);
            default:
                return $response;
        }
    }
}
